package com.flyco.modules.report.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.report.entity.ReportSqlConfig;
import com.flyco.modules.report.mapper.ReportSqlConfigMapper;
import com.flyco.modules.report.service.IReportSqlConfigService;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import com.google.common.base.Joiner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Api(tags = "销售报表统计")
@RestController
@RequestMapping("/f/report/salesReport")
public class SalesReportController {
    @Autowired
    private IReportSqlConfigService reportSqlConfigService;
    @Autowired
    private ReportSqlConfigMapper mapper;
    @Autowired
    private IStmStoreInfoService stmStoreInfoService;

    /**
     * 通过code查询
     *
     * @param code
     * @return
     */
    @AutoLog(value = "销售报表统计-通过code查询")
    @ApiOperation(value="销售报表统计-通过code查询", notes="销售报表统计-通过code查询")
    @GetMapping(value = "/queryByCode")
    public Result<?> queryByCode(@RequestParam(name="code",required=true) String code) {
        ReportSqlConfig reportSqlConfig = reportSqlConfigService.queryByCode(code);
        if(null != reportSqlConfig)
            return Result.ok(reportSqlConfig);
        else
            return Result.error("没有记录");
    }

    /**
     * 通过code查询报表数据
     *
     * @param code
     * @return
     */
    @AutoLog(value = "销售报表统计-通过code查询报表数据")
    @ApiOperation(value="销售报表统计-通过code查询报表数据", notes="销售报表统计-通过code查询报表数据")
    @GetMapping(value = "/queryDataSourceByCode")
    public Result<?> queryDataSourceByCode(@RequestParam(name="code",required=true) String code, @RequestParam(name="storeId",required=false) String storeId,  @RequestParam(name="startTime",required=true) String startTime, @RequestParam(name="endTime",required=true) String endTime) {
        ReportSqlConfig reportSqlConfig = reportSqlConfigService.queryByCode(code);
        if(null != reportSqlConfig && null!=reportSqlConfig.getParamData()) {
            String sql = String.valueOf(reportSqlConfig.getParamData());
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            sql = sql.replace("{time_start}", startTime + " 00:00:00").replace("{time_end}", endTime + " 23:59:59");
            sql = sql.replace("{partner_id}",String.valueOf(sysUser.getPartnerId()));
            if (oConvertUtils.isNotEmpty(storeId)) {
                sql = sql.replace("{store_id}",storeId);
            } else {
                List<StmStoreInfo> storeInfos = stmStoreInfoService.list();
                List<Long> storeIds = storeInfos.stream()
                        .filter(r-> !Objects.equals(0L,r.getStoreId()))
                        .map(s->s.getStoreId())
                        .collect(Collectors.toList());
                if (!"store".equalsIgnoreCase(reportSqlConfig.getDataSource())) {
                    storeIds.add(0L);
                }
                log.info("店铺：" + storeIds);
                String ids = Joiner.on(",").join(storeIds);
                sql = sql.replace("{store_id}",ids);
            }
            //log.info(sql);
            List<Object> objectList;
            if ("store".equalsIgnoreCase(reportSqlConfig.getDataSource())) {
                objectList = mapper.executeSqlFromStore(sql);
            } else {
                objectList = mapper.executeSql(sql);
            }
            return Result.ok(objectList);
        }
        else
            return Result.error("没有记录");
    }


    /**
     * 查询可显示的配置
     *
     * @return
     */
    @AutoLog(value = "销售报表统计-查询可显示的配置")
    @ApiOperation(value="销售报表统计-查询可显示的配置", notes="销售报表统计-查询可显示的配置")
    @GetMapping(value = "/queryShowConfig")
    public Result<?> queryShowConfig(@RequestParam(name="url",required=false) String url) {
        log.info("url:" + url);
        List<ReportSqlConfig> configList = reportSqlConfigService.queryShowConfig(url);
        if(null != configList )
            return Result.ok(configList);
        else
            return Result.error("没有记录");
    }






}
